package cn.wolfcode.luowowo.website.advice;

import cn.wolfcode.luowowo.common.exception.LoginException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// controller的功能增强标签
// 对请求方法处理前, 处理后动态增加功能
// 请求方法处理前: 运用场景: 统一时间参数格式
// 请求方法处理后: 运用场景: 统一异常处理
@ControllerAdvice
public class CommonExceptionHandler {

    // 请求出异常之后, 抛出LoginException 统一交给这个方法处理
    @ExceptionHandler(LoginException.class)
    @ResponseBody
    public Object displayExp(Exception e, Model model, HttpServletResponse resp) throws IOException {
        e.printStackTrace();
        return new AjaxResult(false, e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object RunTimeExp(Exception e, HttpServletResponse resp) throws IOException{
        e.printStackTrace();
        return new AjaxResult(false, "系统繁忙, 请联系管理员");
    }
}
